$(window).load(function(){
	/* 获取localstorage里保险公司信息，渲染DOM结构 */
	var companyList = JSON.parse(localStorage.getItem("eBizCompanyList"));
	if(companyList){
		for(var i=0;i<companyList.length;i++){
			var companyListData = $('<li class="isflexbox"><div class="company-left isflexbox isflex"><img class="company-img" src="http://qa.ydche.com/media/mcimg/'+companyList[i].imageName+'" /><p>'+companyList[i].insurerName+'</p><div class="scheme-info isflexbox"><img src="images/scheme_website.png" /><span>网点</span></div><div class="scheme-info isflexbox"><img src="images/scheme_serve.png" /><span>服务</span></div></div><img class="company-right" src="images/user_on.png" /><input type="radio" value="" insurercode="'+companyList[i].insurerCode+'" checked /></li>');
			companyListData.appendTo($(".scheme-company ul"));
		}
	}else{
		warnText($(".red-bg"),"未获取到保险公司列表！",1000);
	}

	

	/* 选择按钮 */
	//单独点击选择保险公司
	$(".scheme-company ul").delegate(" li", "click", function(){
		if($(this).find("input").prop("checked")){
			$(this).find("input").prop("checked",false);
			$(this).find(".scheme-info").hide();
			$(this).find(".company-right").attr("src", "images/user_un.png");
			
		}else{
			$(this).find("input").prop("checked",true);
			$(this).find(".scheme-info").show();
			$(this).find(".company-right").attr("src", "images/user_on.png");
		}
		
		//是否选择了所有保险公司
		if($(".scheme-company ul").find("input:checked").length == $(".scheme-company ul").find("input").length){
			$(".company-all").find("input").prop("checked",true);
			$(".company-all").find("img").attr("src", "images/user_on.png");
		}else{
			$(".company-all").find("input").prop("checked",false);
			$(".company-all").find("img").attr("src", "images/user_un.png");
		}
	})

	// 全选按钮函数
	function choiceWhole(){
		if($(".company-all").find("input").prop("checked")){
			$(".company-all").find("input").prop("checked",false);
			$(".company-all").find("img").attr("src", "images/user_un.png");
			$(".company-all").siblings("ul").find("input").prop("checked",false);
			$(".company-all").siblings("ul").find(".scheme-info").hide();
			$(".company-all").siblings("ul").find(".company-right").attr("src", "images/user_un.png");
		}else{
			$(".company-all").find("input").prop("checked",true);
			$(".company-all").find("img").attr("src", "images/user_on.png");
			$(".company-all").siblings("ul").find("input").prop("checked",true);
			$(".company-all").siblings("ul").find(".scheme-info").show();
			$(".company-all").siblings("ul").find(".company-right").attr("src", "images/user_on.png");
		}
	}

	// 全选按钮处理
	$(".company-all span").on("click", function(){
		choiceWhole();
	})

	$(".company-all img").on("click", function(){
		choiceWhole();
	})



	/* 选择保险公司报价 */
	var choiceCompanyArr = new Array();
	var companyListArr = new Array();
	$(".company-next p").on("click", function(){
		choiceCompanyArr = [];
		companyListArr = [];
		var checkedCompanyLength = $(".scheme-company ul li").find("input[type='radio']:checked").length;
		if(checkedCompanyLength == 0){
			warnText($(".red-bg"),"请选择至少一家保险公司！",1000);
			return false;
		}else{
			$(".scheme-company ul li").find("input[type='radio']").each(function(){
				if($(this).prop("checked")){
					var choiceCompanyObj = new Object();
					choiceCompanyObj.imagename = $(this).siblings(".company-left").find("p").text();
					choiceCompanyObj.insurername = $(this).siblings(".company-left").find(".company-img").attr("src");
					choiceCompanyObj.insurercode = $(this).attr("insurercode");
					choiceCompanyArr.push(choiceCompanyObj);
					companyListArr.push($(this).attr("insurercode"));
				}
			})
			window.localStorage.setItem("eBizChoiceCompanyMsg", JSON.stringify(choiceCompanyArr));
			window.localStorage.setItem("eBizChoiceCompanyList", JSON.stringify(companyListArr));
			window.location.href = "bizresult.html";
		}
	})
})